package com.rljk.controller.app;/**
 * Created by mac on 2018/8/13.
 */

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.CommentInManager;
import com.rljk.service.HeadlinesManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.RelativeDateFormat;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CommentInController
 * @Description
 * @Author zhang
 * @Date 2018/8/13 18:39
 */
@RestController
@Slf4j
@RequestMapping("/comment")
public class CommentInController extends BaseController {

    @Resource(name = "commentInService")
    private CommentInManager commentInService;

    @Resource(name = "headlinesService")
    private HeadlinesManager headlinesService;


    @Autowired
    RedisService redisService;


    /**
     * @Author zhang
     * @Description  获取评论列表
     * @Date 2018/8/13 18:43
     * @Param [request]
     * @return java.lang.Object
     **/
    @RequestMapping(value="/commentlist")
    public Object commentList() {
        log.info("获取评论列表");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("COMMENTLIST", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("commentList",pd)){

//                    pd.put("IS_SHOW",1);
                    List<PageData> commentList = commentInService.getCommentListById(pd);

                    if (commentList.size() != 0){
                        for(PageData p : commentList){

                            String commenttime = (String)p.get("COMMENTTIME");
                            String format = RelativeDateFormat.format(commenttime);
                            p.put("COMMENTTIME",format);

                        }
                        return Result.success(commentList);
                    }else {
                        return Result.success(commentList);
                    }

                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * @Author zhang
     * @Description 通过用户ID获取自己所属文章的评论列表
     * @Date 2018/8/23 18:25
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/usercommentlist")
    public Object findCommentListByUserId() {
        log.info("通过用户ID获取自己所属文章的评论列表");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("USERCOMMENTLIST", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("findCommentListByUserId",pd)){
                    String token = redisService.get((String) pd.get("TOKEN"));
                    pd.put("USER_ID",token);

                    log.info("================="+pd.toString());

                    List<PageData> commentList = commentInService.findCommentListByUserId(pd);

                    for (PageData pageData : commentList){
                        String commenttime = (String)pageData.get("COMMENTTIME");
                        String format = RelativeDateFormat.format(commenttime);
                        pageData.put("COMMENTTIME",format);
                    }

                    return Result.success(commentList);

                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * @Author zhang
     * @Description  添加评论
     * @Date 2018/8/14 10:45
     * @Param [request]
     * @return java.lang.Object
     **/
    @RequestMapping(value="/addcomment")
    public Object addComment(HttpServletRequest request) {
        log.info("添加评论");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("ADDCOMMENT", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("addComment",pd)){

                    String newsdetail_id = (String)pd.get("NEWSDETAIL_ID");

                    String  token = (String) pd.get("TOKEN");
                    String userId = redisService.get(token);
                    pd.put("USER_ID",userId);
                    pd.put("COMMENT_ID",this.getSnowId());

                    commentInService.save(pd);

                    //修改评论数量
                    pd.put("NEWSDETAIL_ID",newsdetail_id);
                    PageData headlinesInfo = headlinesService.findById(pd);
                    Integer comment_num = (Integer)headlinesInfo.get("COMMENT_NUM");
                    pd.put("COMMENT_NUM",comment_num + 1);
                    headlinesService.edit(pd);

                    return Result.success(null);

                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


}
